#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define BUFSZ 4096
int main(int argc, char** argv){
	if(argc!=2){
		fprintf(stderr,"Usage: clearDisk device\n");
		exit(1);
	}
	int fd;
	if( (fd = open(argv[1],O_WRONLY))<0 ){
		fprintf(stderr,"fail to open file: %s\n",argv[1]);
		exit(1);
	}
	long total_size = lseek(fd, 0, SEEK_END);
	if(total_size < 0){
		fprintf(stderr, "fail to lseek in file: %s\n", argv[1]);
		exit(1);
	}
	int total_blocks = (total_size + BUFSZ - 1) / BUFSZ;
	lseek(fd, 0, SEEK_SET);
	char buf[BUFSZ] = {0};
	for(int i = 0 ; i < total_blocks; i++){
		if( write(fd, buf, BUFSZ) < 0){
			fprintf(stderr, "fail to write in file: %s\n", argv[1]);
			exit(1);
		}
		printf("clearing disk... %%%f completed\r",(float)i * 100 / total_blocks);
	}
	return 0;
}
